package io.renren.config;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import io.renren.modules.sys.entity.SysUserEntity;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(HttpServletRequest request) {
        {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            List<ISqlParser> sqlParserList = new ArrayList<>();
            TenantSqlParser tenantSqlParser = new TenantSqlParser();
            tenantSqlParser.setTenantHandler(new TenantHandler() {
                @Override
                public Expression getTenantId(boolean where) {
                    //从session中获取租户id
                    SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
                    if(user.getTenantId()!=null){
                        return new LongValue(user.getTenantId());
                    } else {
                        return null;
                    }
                }

                @Override
                public String getTenantIdColumn() {
                    return "tenant_id";
                }

                @Override
                public boolean doTableFilter(String tableName) {
                    //获取开启状态

                        //从session中获取租户id
                        String loginName = null;
                        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
                        if(user==null){
                            return true;
                        }
                        if(user != null) {
                            loginName = user.getUsername();
                        }
                        if(("admin").equals(loginName)) {
                            return true;
                        } else {
//                        // 这里可以判断是否过滤表
                            if ("jsh_material_property".equals(tableName) || "jsh_sequence".equals(tableName)
                                    || "jsh_platform_config".equals(tableName)|| "jsh_tenant".equals(tableName)
                                    || "sys_role_menu".equals(tableName)|| "sys_user_token".equals(tableName)
                                    || "sys_log".equals(tableName)|| "sys_config".equals(tableName)
                                    || "sys_oss".equals(tableName)|| "sys_user_role".equals(tableName)
                                    || "sys_captcha".equals(tableName)|| "sys_menu".equals(tableName)
                                    ||"ACT_ADM_DATABASECHANGELOG".equals(tableName)||"ACT_ADM_DATABASECHANGELOGLOCK".equals(tableName)
                                    ||"ACT_ADM_SERVER_CONFIG".equals(tableName)||"ACT_EVT_LOG".equals(tableName)
                                    ||"ACT_GE_BYTEARRAY".equals(tableName)||"ACT_GE_PROPERTY".equals(tableName)
                                    ||"ACT_HI_ACTINST".equals(tableName)||
                                    "ACT_HI_ATTACHMENT".equals(tableName)||
                                    "ACT_HI_COMMENT".equals(tableName)||
                                    "ACT_HI_DETAIL".equals(tableName)||
                                    "ACT_HI_ENTITYLINK".equals(tableName)||
                                    "ACT_HI_IDENTITYLINK".equals(tableName)||
                                    "ACT_HI_PROCINST".equals(tableName)||
                                    "ACT_HI_TASKINST".equals(tableName)||
                                    "ACT_HI_TSK_LOG".equals(tableName)||
                                    "ACT_HI_VARINST".equals(tableName)||
                                    "ACT_ID_BYTEARRAY".equals(tableName)||
                                    "ACT_ID_GROUP".equals(tableName)||
                                    "ACT_ID_INFO".equals(tableName)||
                                    "ACT_ID_MEMBERSHIP".equals(tableName)||
                                    "ACT_ID_PRIV".equals(tableName)||
                                    "ACT_ID_PRIV_MAPPING".equals(tableName)||
                                    "ACT_ID_PROPERTY".equals(tableName)||
                                    "ACT_ID_TOKEN".equals(tableName)||
                                    "ACT_ID_USER".equals(tableName)||
                                    "ACT_PROCDEF_INFO".equals(tableName)||
                                    "ACT_RE_DEPLOYMENT".equals(tableName)||
                                    "ACT_RE_MODEL".equals(tableName)||
                                    "ACT_RE_PROCDEF".equals(tableName)||
                                    "ACT_RU_ACTINST".equals(tableName)||
                                    "ACT_RU_DEADLETTER_JOB".equals(tableName)||
                                    "ACT_RU_ENTITYLINK".equals(tableName)||
                                    "ACT_RU_EVENT_SUBSCR".equals(tableName)||
                                    "ACT_RU_EXECUTION".equals(tableName)||
                                    "ACT_RU_EXTERNAL_JOB".equals(tableName)||
                                    "ACT_RU_HISTORY_JOB".equals(tableName)||
                                    "ACT_RU_IDENTITYLINK".equals(tableName)||
                                    "ACT_RU_JOB".equals(tableName)||
                                    "ACT_RU_SUSPENDED_JOB".equals(tableName)||
                                    "ACT_RU_TASK".equals(tableName)||
                                    "ACT_RU_TIMER_JOB".equals(tableName)||
                                    "ACT_RU_VARIABLE".equals(tableName)||
                                    "FLW_CHANNEL_DEFINITION".equals(tableName)||
                                    "FLW_EV_DATABASECHANGELOG".equals(tableName)||
                                    "FLW_EV_DATABASECHANGELOGLOCK".equals(tableName)||
                                    "FLW_EVENT_DEFINITION".equals(tableName)||
                                    "FLW_EVENT_DEPLOYMENT".equals(tableName)||
                                    "FLW_EVENT_RESOURCE".equals(tableName)||
                                    "FLW_RU_BATCH".equals(tableName)||
                                    "FLW_RU_BATCH_PART".equals(tableName)||
                                    "T_FLOWABLE_FORM".equals(tableName)||
                                    "t_sys_post".equals(tableName)
                            ) {
                                return true;
                            } else {
                                return false;
                            }
                        }

                }
            });

            sqlParserList.add(tenantSqlParser);
            paginationInterceptor.setSqlParserList(sqlParserList);
            paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
                @Override
                public boolean doFilter(MetaObject metaObject) {
                    MappedStatement ms = SqlParserHelper.getMappedStatement(metaObject);
                    // 过滤自定义查询此时无租户信息约束出现
                    if ("io.renren.modules.sys.dao.SysCaptchaDao.selectOne".equals(ms.getId())||
                            "io.renren.modules.sys.dao.SysCaptchaDao.insert".equals(ms.getId())||
                            "io.renren.modules.erp.dao.DepotItemMapperEx.getStockByParam".equals(ms.getId())) {
                        return true;
                    }
                    return false;
                }
            });
            return paginationInterceptor;
        }
    }

}
